from urllib import request, parse
from lxml import etree
def loadPage(url):
    print(1)
	# 发起请求
    req = request.Request(url)
	# 打开响应的对象
    response = request.urlopen(req)
	# 获取响应的内容
    try:
        html = response.read()
    except Exception as e:
        html = e.partial
	# 对网页进行解码
    content = html.decode('utf-8')
	
	# 使用etree对html的内容建立文档树
    content = etree.HTML(content)
    link_list = content.xpath('//div/a[@class="j_th_tit "]/@href')
    for link in link_list:
        fulllink = 'https://tieba.baidu.com' + link
        loadImage(fulllink)
# 加载帖子中的图片的链接
def loadImage(url):
    print(2)
	# 发起请求
    req = request.Request(url)
	# 打开响应的对象
    response = request.urlopen(req)
	# 获取响应的内容
    html = response.read()
	# 对网页进行解码
    content = html.decode('utf-8')
	# 使用etree对html的内容建立文档树
    content = etree.HTML(content)
    link_list = content.xpath('//img[@class="BDE_Image"]/@src')
    for link in link_list:
        print(link)
        writeImage(link)
# 把图片下载并保存到本地
def writeImage(url):
	# 发起请求
    req = request.Request(url)
	# 打开响应的对象
    response = request.urlopen(req)
	# 获取响应的内容 下载图片获得的是二进制字节码
    try:
        image = response.read()
    except Exception as e:
        image = e.partial
    filename = url[-15:]
    f = open('img/' + filename, 'wb')
    f.write(image)
    f.close()
if __name__ == '__main__':
    url = 'https://tieba.baidu.com/f?kw=%E6%9F%AF%E5%8D%97&pn=0'
    loadPage(url)

